package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.MasterCancelFolloweBean;
import com.example.administrator.yuejian_2.bean.MasterDetailBean;
import com.example.administrator.yuejian_2.bean.MasterFollowBean;
import com.example.administrator.yuejian_2.bean.MasterIsFollowedBean;
import com.example.administrator.yuejian_2.fragment.MaAnswerFragment;
import com.example.administrator.yuejian_2.fragment.MaClassFragment;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.FollowService;
import com.example.administrator.yuejian_2.interservice.MasterService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MaDetailActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private MaClassFragment maClassFragment;
    private MaAnswerFragment maAnswerFragment;
    private android.support.v4.app.FragmentManager fragmentManager;
    private android.support.v4.app.FragmentTransaction fragmentTransaction;

    private ImageView mapic;
    private TextView maintro;
    private TextView maaddress;
    private int mid;
    private int uid;
    private int sta;
    private Context context;
    private ImageButton collectButton;
    private ImageButton backButton;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_madetail);


        mid = getIntent().getIntExtra("mid",-1);

        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        uid = sharedPreferences.getInt("user_id",0);

        mapic = (ImageView) findViewById(R.id.ma_imageView3);
        maintro = (TextView) findViewById(R.id.maintro_textView5);
        maaddress = (TextView) findViewById(R.id.masddress_textView15);

        getmadetail();

        ismafollowed();

        collectButton = (ImageButton) findViewById(R.id.collect_imageButton6);
        collectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (sta == 0){
                    mafollow();
                }else {
                    manofollow();
                }

            }
        });


        backButton = (ImageButton) findViewById(R.id.back_imageButton5);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        fragmentManager = getSupportFragmentManager();
        radioGroup = (RadioGroup) findViewById(R.id.ma_radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId) {
                    case R.id.ma_class:
                        maClassFragment = new MaClassFragment();
                        replaceFragment(maClassFragment);
                        break;
                    case R.id.ma_answer:
                        maAnswerFragment = new MaAnswerFragment();
                        replaceFragment(maAnswerFragment);
                        break;
                }

            }
        });
        radioGroup.check(R.id.ma_class);
    }

    private void replaceFragment(android.support.v4.app.Fragment fragment) {
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.ma_fragmentholder,fragment);
        fragmentTransaction.commit();
    }

    private void getmadetail(){
        context = MaDetailActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MasterService service = retrofit.create(MasterService.class);
        Call<MasterDetailBean> call = service.getmaster(mid);
        call.enqueue(new Callback<MasterDetailBean>() {
            @Override
            public void onResponse(Call<MasterDetailBean> call, Response<MasterDetailBean> response) {
                if (response.body()!= null){
                    String intro = response.body().getC_intro();
                    String address = response.body().getClub_location();
                    String pic = response.body().getC_pic();

                    maintro.setText(intro);
                    maaddress.setText(address);
                    if (!pic.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/photo/"+pic)
                                .into(mapic);
                }
            }

            @Override
            public void onFailure(Call<MasterDetailBean> call, Throwable t) {

            }
        });
    }

    private void ismafollowed(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FollowService service = retrofit.create(FollowService.class);
        Call<MasterIsFollowedBean> call = service.ismafollow(uid,mid);
        call.enqueue(new Callback<MasterIsFollowedBean>() {
            @Override
            public void onResponse(Call<MasterIsFollowedBean> call, Response<MasterIsFollowedBean> response) {
                if (response.body().isInfo() == true){
                    collectButton.setImageResource(R.drawable.ic_collected);
                    sta = 1;
                }else {
                    collectButton.setImageResource(R.drawable.ic_collect);
                    sta = 0;
                }

            }

            @Override
            public void onFailure(Call<MasterIsFollowedBean> call, Throwable t) {

            }
        });
    }

    private void mafollow(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FollowService service = retrofit.create(FollowService.class);
        Call<MasterFollowBean> call = service.maFollow(uid,mid);
        call.enqueue(new Callback<MasterFollowBean>() {
            @Override
            public void onResponse(Call<MasterFollowBean> call, Response<MasterFollowBean> response) {
                if (response.body().isInfo() == true){
                    collectButton.setImageResource(R.drawable.ic_collected);
                    Toast.makeText(getApplicationContext(),"收藏成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<MasterFollowBean> call, Throwable t) {

            }
        });
    }

    private void manofollow(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FollowService service = retrofit.create(FollowService.class);
        Call<MasterCancelFolloweBean> call = service.manofollow(uid,mid);
        call.enqueue(new Callback<MasterCancelFolloweBean>() {
            @Override
            public void onResponse(Call<MasterCancelFolloweBean> call, Response<MasterCancelFolloweBean> response) {
                if (response.body().isInfo() == true){
                    collectButton.setImageResource(R.drawable.ic_collect);
                    Toast.makeText(getApplicationContext(),"取消收藏成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<MasterCancelFolloweBean> call, Throwable t) {

            }
        });
    }




}
